package controller;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import dao.Thanhviendao;

import model.Thanhvienmodel;

public class Thanhviencontroller extends HttpServlet{
	protected void doPost(HttpServletRequest req,HttpServletResponse rep){
		try {
			String actionType=req.getParameter("actionType");
			String pageNext="";
			if(actionType.equals("addNew")){
				
				String TENDN=req.getParameter("txttdn");
				String MATKHAU=req.getParameter("txtmk");
				String VAITRO=req.getParameter("txtvt");
				String GHICHU=req.getParameter("txtgc");
				
				Thanhvienmodel model=new Thanhvienmodel();
				model.setTENDN(TENDN);
				model.setMATKHAU(MATKHAU);
				model.setVAITRO(VAITRO);
				model.setGHICHU(GHICHU);
				
				//Boolean inserttatus=Thanhviendao.Themthanhvien(TENDN, MATKHAU, VAITRO, GHICHU);
				Boolean inserttatus=Thanhviendao.Themthanhvien(model);
				if(inserttatus == true){
					pageNext="thanhcong.jsp";
					
				}
				else{
					pageNext="Themmoi.jsp";
				}
				RequestDispatcher rd=req.getRequestDispatcher(pageNext);
				rd.forward(req, rep);
			}
			
			if (actionType.equals("updateThanhVien")) {
				String ID = req.getParameter("txtid");
				String TENDN=req.getParameter("txttdn");
				String MATKHAU=req.getParameter("txtmk");
				String VAITRO=req.getParameter("txtvt");
				String GHICHU=req.getParameter("txtgc");
				
				Thanhvienmodel model=new Thanhvienmodel();
				model.setID(ID);
				model.setTENDN(TENDN);
				model.setMATKHAU(MATKHAU);
				model.setVAITRO(VAITRO);
				model.setGHICHU(GHICHU);
				Boolean kq = Thanhviendao.updateThanhVien(model);
				if (kq) {
					pageNext="thanhcong.jsp";
				} else {
					pageNext="Themmoi.jsp";
				}
				RequestDispatcher rd=req.getRequestDispatcher(pageNext);
				rd.forward(req, rep);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
	}
protected void doGet(HttpServletRequest req,HttpServletResponse rep){
		doPost(req,rep);
	}

}
